#[cfg(test)]
pub mod tests {
    use anyhow::anyhow;
    use anyhow::{Ok, Result};
    use log::info;
    use rbase::db::{CONNECTION, RT};
    use sea_orm::Database;

    use crate::db;

    pub async fn test_init() -> Result<()> {
        db::init().await?;
        Ok(())
    }

    pub async fn test_common_init() -> Result<()> {
        {
            let mut con = CONNECTION.lock().map_err(|e| anyhow!("Error locking connection: {}", e))?;

            let connection = std::thread::spawn(move || RT.handle().block_on(async move { Database::connect("sqlite::memory:").await.unwrap() }))
                .join()
                .map_err(|e| anyhow!("Error joining thread: {:?}", e))?;

            info!("Database connection established");
            con.replace(connection);
        }

        db::init().await?;

        Ok(())
    }
}
